package com.zoe.app.config;

import cn.hutool.core.date.DateUtil;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import org.springframework.boot.autoconfigure.jackson.Jackson2ObjectMapperBuilderCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.math.BigInteger;
import java.util.TimeZone;

/**
 * @author Zoe.Zhang
 * @date 2021/09/25
 * @description 设置全局的时间格式化配置/Long传给前端丢失经度问题
 */
@Configuration
public class JacksonConfig {
    @Bean
    Jackson2ObjectMapperBuilderCustomizer jackson2ObjectMapperBuilderCustomizer() {
        // Jackson2ObjectMapperBuilderCustomizer jackson2ObjectMapperBuilderCustomizer = new Jackson2ObjectMapperBuilderCustomizer() {
        //     /**
        //      * Customize the JacksonObjectMapperBuilder.
        //      *
        //      * @param jacksonObjectMapperBuilder the JacksonObjectMapperBuilder to customize
        //      */
        //     @Override
        //     public void customize(Jackson2ObjectMapperBuilder jacksonObjectMapperBuilder) {
        //         jacksonObjectMapperBuilder.dateFormat(DateUtil.newSimpleFormat("yyyy-MM-dd HH:mm:ss"));
        //         jacksonObjectMapperBuilder.serializerByType(Long.class, ToStringSerializer.instance);
        //     }
        // };
        Jackson2ObjectMapperBuilderCustomizer jackson2ObjectMapperBuilderCustomizer = (x) -> {
            x.timeZone(TimeZone.getDefault());
            // 全局的时间格式化;如果在列上面指定了格式化格式,则会覆盖该时间配置
            x.dateFormat(DateUtil.newSimpleFormat("yyyy-MM-dd HH:mm:ss"));
            // Long传给前端经度丢失问题 - Long转String
            x.serializerByType(Long.class, ToStringSerializer.instance);
            x.serializerByType(BigInteger.class, ToStringSerializer.instance);
        };

        return jackson2ObjectMapperBuilderCustomizer;
    }
}
